package com.Glow.reggie.controller;

import com.Glow.reggie.common.BaseContext;
import com.Glow.reggie.common.R;
import com.Glow.reggie.entity.AddressBook;
import com.Glow.reggie.service.AddressBookService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Glow
 * @Date 2022-08-05 16:07:28
 * @Description
 * @Version 1.0
 */
@RestController
@RequestMapping("addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook)
    {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }
    @GetMapping("list")
    public R<List<AddressBook>> list(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return R.success(list);
    }
    @PutMapping("default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook)
    {
        LambdaUpdateWrapper<AddressBook> chainWrapper =  new LambdaUpdateWrapper<>();
        chainWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        chainWrapper.set(AddressBook::getIsDefault,0);
        addressBookService.update(chainWrapper);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }
    @GetMapping("default")
    public R<AddressBook> defaults()
    {
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<AddressBook>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,addressBook.getUserId());
        queryWrapper.eq(AddressBook::getIsDefault,1);
        addressBook = addressBookService.getOne(queryWrapper);
        return R.success(addressBook);
    }
    @GetMapping("{id}")
    public R<AddressBook> getDefault(@PathVariable Long id)
    {
        AddressBook addressBook = new AddressBook();
       addressBook =addressBookService.getById(id);
       return R.success(addressBook);
    }
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook)
    {
        addressBookService.updateById(addressBook);
        return R.success("修改地址簿成功！");
    }
    @DeleteMapping
    public R<String> remove(Long ids)
    {
        addressBookService.removeById(ids);
        return R.success("删除地址簿成功！");
    }
}
